package com.sugar.follow.up.module.business.controller.admin.patient.gzxss;

import com.sugar.follow.up.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.follow.up.module.business.controller.admin.patient.gzxss.vo.PatientGzxssPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.patient.gzxss.vo.PatientGzxssRespVO;
import com.sugar.follow.up.module.business.controller.admin.patient.gzxss.vo.PatientGzxssSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.dal.dataobject.patientgzxss.PatientGzxssDO;
import com.sugar.follow.up.module.business.service.patientgzxss.PatientGzxssService;

@Tag(name = "管理后台 - 根治性手术")
@RestController
@RequestMapping("/business/patient-gzxss")
@Validated
public class PatientGzxssController {

    @Resource
    private PatientGzxssService patientGzxssService;

    @PostMapping("/create")
    @Operation(summary = "创建根治性手术")
    //@PreAuthorize("@ss.hasPermission('business:patient-gzxss:create')")
    public CommonResult<Long> createPatientGzxss(@Valid @RequestBody PatientGzxssSaveReqVO createReqVO) {
        return success(patientGzxssService.createPatientGzxss(createReqVO, SecurityFrameworkUtils.getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新根治性手术")
    //@PreAuthorize("@ss.hasPermission('business:patient-gzxss:update')")
    public CommonResult<Boolean> updatePatientGzxss(@Valid @RequestBody PatientGzxssSaveReqVO updateReqVO) {
        patientGzxssService.updatePatientGzxss(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除根治性手术")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('business:patient-gzxss:delete')")
    public CommonResult<Boolean> deletePatientGzxss(@RequestParam("id") Long id) {
        patientGzxssService.deletePatientGzxss(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除根治性手术")
                //@PreAuthorize("@ss.hasPermission('business:patient-gzxss:delete')")
    public CommonResult<Boolean> deletePatientGzxssList(@RequestParam("ids") List<Long> ids) {
        patientGzxssService.deletePatientGzxssListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得根治性手术")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('business:patient-gzxss:query')")
    public CommonResult<PatientGzxssRespVO> getPatientGzxss(@RequestParam("id") Long id) {
        PatientGzxssDO patientGzxss = patientGzxssService.getPatientGzxss(id);
        return success(BeanUtils.toBean(patientGzxss, PatientGzxssRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得根治性手术分页")
    //@PreAuthorize("@ss.hasPermission('business:patient-gzxss:query')")
    public CommonResult<PageResult<PatientGzxssRespVO>> getPatientGzxssPage(@Valid PatientGzxssPageReqVO pageReqVO) {
        PageResult<PatientGzxssDO> pageResult = patientGzxssService.getPatientGzxssPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PatientGzxssRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出根治性手术 Excel")
    //@PreAuthorize("@ss.hasPermission('business:patient-gzxss:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPatientGzxssExcel(@Valid PatientGzxssPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PatientGzxssDO> list = patientGzxssService.getPatientGzxssPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "根治性手术.xls", "数据", PatientGzxssRespVO.class,
                        BeanUtils.toBean(list, PatientGzxssRespVO.class));
    }

}